from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

# import requests
# import json
import time
# from fake_useragent import UserAgent
from lxml import etree
import re
import os
import random
import logging
import redis
# from .gt_cookies import open_cookie_file
# from .information import USER,PASSWORD
from ip_port import checkout_time
from make_ua import random_ua,random_mobile_ua
from read_ua import read_ua_item,make_js_str
logger = logging.getLogger(__name__)
fh = logging.FileHandler('kuaipai.log')
sh = logging.StreamHandler()
logger.addHandler(fh)
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)

mdb = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=True, password='ma1114')

def open_cookie_file(file, moshi):
    if moshi == 'a':
        with open(file, 'r') as f:
            con = f.read()
            lis = re.findall(r'.*?-+.*?-+(.*?)\n', con)
            return lis
    elif moshi == 'c':
        with open(file, 'r') as f:
            con = f.read()
            lis = re.findall(r'.*?-+.*?-+.*?-+.*?-+(.*?)\n', con)
            return lis
    else:
        with open(file, 'r') as f:
            con = f.read()
            lis = con.split('\n')
            return lis


def time_ge():
    with open(r'time_ge.txt', 'r') as f:
        con = f.read()
    return con.split(',')

def make_li(zhijie, jingguo, jiezhi):
    li_moshi = []

    if str(zhijie).isdigit():
        for i in range(1, int(zhijie) + 1):
            li_moshi.append('zhijie')

    if str(jingguo).isdigit():
        for i in range(1, int(jingguo) + 1):
            li_moshi.append('jingguo')

    if str(jiezhi).isdigit():
        for i in range(1, int(jiezhi) + 1):
            li_moshi.append('jiezhi')

    return li_moshi

def random_pop(li):
    while True:
        if li:
            value = random.choice(li)
            print(value)
            li.remove(value)
        else:
            break

def run_kuaipai(*args):
    keyword, url_host, hostName, timeout, ipNumber, zhijie, jingguo, jiezhi,tup = args
    a = 0
    value = ''
    list_num = []
    if str(ipNumber).isdigit():
        shu = int(ipNumber)
    else:
        shu = random.randint(8, 13)

    moshi_li = make_li(zhijie, jingguo, jiezhi)  # 生成不同类模式列表 例如['a','a','a','a','b','b','c']
    while True:
        time_now = time.strftime("%H", time.localtime())
        if time_now:
            while a <= shu:
                # ip访问间隔
                # time_list = time_ge()
                # t_str = random.choice(time_list)
                # second = 60
                # exec('second=' + t_str)
                # if not lis_a:
                #     time.sleep(second)
                #     time_list.remove(t_str)
                # ip_port = checkout_time(mdb)
                ip_port = ''
                # 模式的随机选择
                if moshi_li:
                    value = random.choice(moshi_li)
                # try:
                k = KuaiPai(ip_port, keyword,0)
                # print(k.get_track(260))
                k.run(url_host, hostName, timeout, value,tup)
                # k.zhiwen()
                # time.sleep(60000)
                # k.exit_chrome()

                # if not lis_a:
                #     a += 1
                #     moshi_li.remove(value)
                #     list_num.append('\u7a0b\u5b50\u6db5')
                # except:
                #     logger.info('{}--ip访问失败'.format(ip_port))
                #     continue

                if len(list_num) >= 6:
                    logger.info('没有找到指定关键词的相关网址，请确认域名输入是否有误，或者重新再试')
                    break
        print('今日本任务运行结束！')
        time.sleep(60 * 10)


class KuaiPai:

    def __init__(self, ip_port, keyword,moshi):
        self.moshi = moshi
        self.rank = 0
        prefs = {
            'profile.default_content_setting_values': {
                'notifications': 2
            }
        }
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--window-size=1366,768')
        # chrome_options.add_experimental_option('prefs', prefs) #禁止浏览器弹窗
        chrome_options.add_experimental_option('useAutomationExtension', False)
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 去掉正在被自动化检测程序控制的提示
        if moshi == 1:
            ua = random.choice(read_ua_item('ua_pc.txt'))
            chrome_options.add_argument('user-agent={}'.format(ua.get('navigator.userAgent')))
        else:
            ua = random.choice(read_ua_item('ua_m.txt'))
            print(ua)
            chrome_options.add_argument('user-agent={}'.format(ua.get('navigator.userAgent')))
        self.js_canshu = make_js_str(ua)
        # chrome_options.add_argument('--proxy-server=http://{}'.format(ip_port))
        # chrome_options.add_argument('--proxy-server=http://175.4.186.84:21289')
        # chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-plugins')
        self.driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
        self.js = 'Object.defineProperty(navigator,"platform",{get:function(){return "Linux armv8l";}});'
        self.js2 = '''
                        Object.defineProperty(navigator, "platform", {
                            get: function () {
                                return "czh";
                            }
                         }); 
                         Object.defineProperty(navigator, "plugins", {
                            get: function () {
                                return "";
                            }
                         }); 
                         Object.defineProperty(navigator, "language", {
                            get: function () {
                                return "zh-Hans-CN";
                            }
                         }); 
                        var myScreen = {};       // create new screen object
                         
                        // prefill with all properties of old object
                        for (var i in window.screen) {
                            myScreen[i] = window.screen[i];
                        }
                        window.screen = myScreen;    // replace existing object with mine
                        window.screen.width = 520;  // change properites on mine
                        window.screen.height = 1314;
                        window.screen.availWidth=520;
                        window.screen.availHeight=1314;
                        window.doNotTrack = '20130628'
                    '''

        self.js3 = 'alert(window.doNotTrack);'
        # if moshi==0:
        #     self.driver.execute_script(self.js)
        self.ip_port = ip_port
        self.keyword = keyword

    def run_js_canshu(self):
        try:
            self.driver.execute_script(self.js_canshu)
        except Exception as e:
            print(e)

    def get_interger(self, data):  # 转换数字
        try:
            res = int(data)
        except:
            try:
                res = float(data)
            except:
                res = data
        return res

    def is_empty(self, data):  # 处理数据为空的字段 str转int float
        if isinstance(data, list):
            if data != []:
                jieguo = data[0].strip()
                jieguo = self.get_interger(jieguo)
            else:
                jieguo = ''
        else:
            jieguo = self.get_interger(data)
        return jieguo

    def china_school_guanzhu(self):
        self.driver.get('https://author.baidu.com/home?from=bjh_article&app_id=1615352794074392')
        time.sleep(2)
        try:
            btn_text = self.driver.find_element_by_xpath('//*[@id="uhFollow"]/span')
            content = btn_text.text
            if content == '关注':
                fenshi = self.driver.find_element_by_xpath('//div[@class="pc-user-middle"]/div[@class="pc-user-info"]/div[4]/span')
                fenshi_num = fenshi.text.strip()
                print(fenshi_num)
                if int(fenshi_num) < 5600:
                    btn =  WebDriverWait(self.driver, 10).until(
                                EC.presence_of_element_located((By.ID, 'uhFollow')))
                    time.sleep(1)
                    btn.click()
                    time.sleep(1)
                    if content == '关注':
                        time.sleep(1)
                        btn_text.click()
                else:
                    time.sleep(10)
                    self.driver.quit()
        except Exception as e:
            print(e)

    def take_cookie_2(self, moshi, args):  # 任务之外的方法，点关注所需
        cookies_list_1, cookies_list_2, cookies_list_3, cookies_list_4, cookies_list_5 = args
        # if moshi == 1:
        #     self.driver.get("http://www.baidu.com")
        # else:
        #     self.driver.get("http://m.baidu.com")
        self.china_school_guanzhu()
        # 删除第一次建立连接时的cookie
        self.clear_cookie()
        # 读取登录时存储到本地的cookie
        while True:
            ran = random.randint(1, 101)
            if ran <= 1:
                con = random.choice(cookies_list_1)
                cookies_list_1.remove(con)
            elif 2 <= ran <= 3:
                con = random.choice(cookies_list_2)
                cookies_list_2.remove(con)
            elif 4 <= ran <= 9:
                con = random.choice(cookies_list_3)
                cookies_list_3.remove(con)
            elif 10 <= ran <= 20:
                con = random.choice(cookies_list_4)
                cookies_list_4.remove(con)
            else:
                con = random.choice(cookies_list_5)
                cookies_list_5.remove(con)

            lis = con.split(';')
            try:
                for i in lis:
                    i = i.split('=', 1)
                    self.driver.add_cookie({
                        'domain': '.baidu.com',  # 此处xxx.com前，需要带点
                        'name': i[0],
                        'value': i[-1],
                        'path': '/',
                        'expires': None
                    })
                #     listCookies = json.loads(f.read())
                # self.driver.refresh()
                # time.sleep(1)
                a = self.china_school_guanzhu()
                if a:
                    break
            except Exception:
                logger.info('此cookie失效，已被记录！')
                with open('shixiao_coo.json', 'a') as f:
                    f.write(con + '\n')

    def zhiwen(self):
        self.driver.get("https://tczmh.gitee.io/fingerprint2demo/")
        self.driver.execute_script(self.js_canshu)
        # time.sleep(1)
        # self.driver.execute_script(self.js3)
        time.sleep(2)
        btn = self.driver.find_element_by_xpath('//*[@id="control"]/button')
        btn.click()
        time.sleep(20)

    def take_cookie(self,moshi,args):  # 添加cookies
        cookies_list_1, cookies_list_2, cookies_list_3, cookies_list_4, cookies_list_5 = args
        if moshi == 1:
            self.driver.get("http://www.baidu.com")
        else:
            self.driver.get("http://m.baidu.com")
        # self.driver.execute_script(self.js_canshu)
        # 删除第一次建立连接时的cookie
        self.clear_cookie()
        # 读取登录时存储到本地的cookie
        while True:
            ran = random.randint(1, 101)
            if ran <= 1:
                con = random.choice(cookies_list_1)
            elif 2 <= ran <= 3:
                con = random.choice(cookies_list_2)
            elif 4 <= ran <= 9:
                con = random.choice(cookies_list_3)
            elif 10 <= ran <= 20:
                con = random.choice(cookies_list_4)
            else:
                con = random.choice(cookies_list_5)
            lis = con.split(';')
            try:
                for i in lis:
                    i = i.split('=', 1)
                    self.driver.add_cookie({
                        'domain': '.baidu.com',  # 此处xxx.com前，需要带点
                        'name': i[0],
                        'value': i[-1],
                        'path': '/',
                        'expires': None
                    })
                # self.driver.execute_script(self.js_canshu)
                self.driver.refresh()
                # self.driver.execute_script(self.js_canshu)
                break
            except Exception as e:
                logger.info('此cookie失效，已被记录！{}'.format(e))
                with open('shixiao_coo.json', 'a') as f:
                    f.write(con + '\n')

    def clear_cookie(self):
        self.driver.delete_all_cookies()

    # connect : 宽带拨号
    def connect(self, name, username, password):
        cmd_str = "rasdial %s %s %s" % (name, username, password)
        os.system(cmd_str)
        time.sleep(5)

    # disconnect : 断开宽带连接
    def disconnect(self, name):
        cmd_str = "rasdial %s /disconnect" % name
        os.system(cmd_str)
        time.sleep(5)

    def search(self,driver):
        # js_driver = driver
        try:
            # WebDriverWait(js_driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body > div:nth-child(29)")))
            # driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
            time.sleep(1)
            for i in range(5):
                driver.execute_script("window.scrollTo(0, 50)")
                time.sleep(1)
        except Exception as e:
            print(e)

    def open_baidu(self,moshi, keyword):
        if moshi == 1:
            self.driver.get("http://www.baidu.com")
            time.sleep(3)
            self.driver.find_element_by_xpath("//input[@id='kw']").send_keys(keyword)
            time.sleep(3)
            baidu = self.driver.find_element_by_xpath("//input[@id='su']")
            baidu.click()
            self.driver.implicitly_wait(10)
            time.sleep(1)
        else:
            self.driver.get("http://m.baidu.com")
            time.sleep(6)
            self.driver.find_element_by_xpath("//input[@id='index-kw']").send_keys(keyword)
            time.sleep(3)
            baidu = self.driver.find_element_by_xpath("//*[@id='index-bn']")
            time.sleep(3)
            baidu.click()
            self.driver.implicitly_wait(10)
            time.sleep(1)
        self.driver.execute_script(self.js_canshu)
        time.sleep(2)

    def make_baidu_page(self):
        result = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]/h3/a')
        for i in result:
            time.sleep(3)
            i.click()

    def request_jiezi(self, host_url, name_url):  # 截止模式
        # self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="content_left"]/div[last()]/h3/a')))
        # self.driver.switch_to.window(handles[-1]) #切换到刚打开的窗口
        target_url_id = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        content = self.driver.page_source
        for div in res_divs:
            res, name = '', ''
            target_id = div.get_attribute('id')

            time_now = time.strftime("%H:%M:%S", time.localtime())
            url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/text()'.format(target_id)))
            if not url:
                url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id)))
                if not url:
                    url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
                    if not url:
                        url = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//div[@class="f13  se_st_footer"]/a[1]/text()'.format(target_id)))
                        if not url:
                            url = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//a[@class="c-showurl"]/text()'.format(target_id)))

            if '.' not in url:
                name = url
                url = ''
            if not url:
                name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/span[@class="nor-src-wrap"]/text()'.format(target_id)))
                if not name:
                    name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//a[@class="c-showurl "]/span/text()'.format(target_id)))
                    if not name:
                        name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//span[@class="nor-src-wrap"]/text()'.format(target_id)))
            else:
                # name = '' #重置已匹配的熊掌号
                res = re.findall(r'{}'.format(host_url), url)
                if not res:
                    res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)
            # print(target_id, url, name)
            if res or (name == name_url and name):  # 这段代码可以封装
                time.sleep(3)
                element = WebDriverWait(self.driver, 30).until(
                    EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id))))
                position = element.location_once_scrolled_into_view
                time.sleep(1)
                self.driver.execute_script(
                    "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                time.sleep(random.randint(1, 3))
                element.click()
                time.sleep(random.randint(20, self.timeout+50))
                handles = self.driver.window_handles  # 窗口句柄
                time.sleep(2)
                self.driver.switch_to.window(handles[-1])  # 切换到新打开的窗口
                # time.sleep(random.randint(1,self.timeout+15))
                if random.randint(1, 101)>=30:
                    self.driver.refresh()
                    time.sleep(random.randint(1, self.timeout))
                target_url_id.append(target_id)
                logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                    res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + target_id + ',' + '点击状态:' + '截止模式目标网址点击完成,' + 'time:' + time_now + '}')
                self.driver.close()
                return target_url_id
            else:
                if random.randint(1, 100) % 5 == 0:
                    time.sleep(2)
                    element = WebDriverWait(self.driver, 30).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1, 3))
                    element.click()
                    handles = self.driver.window_handles  # 窗口句柄
                    time.sleep(random.randint(1, 3))
                    self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                    time.sleep(random.randint(1, 2))
                    logger.info(
                        '{ip:' + self.ip_port + ',' + '百度搜索关键词:' + self.keyword + ',' + '点击状态:' + '截止模式非目标网址点击完成,' + 'time:' + time_now + '}')

    def request_jingguo(self, host_url, name_url):  # 经过模式
        target_url_host = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        if not res_divs:
            res_divs = self.driver.find_elements_by_xpath(
                '//*[@id="content_left"]/div[@class="result c-container new-pmd"]')
        if res_divs:
            content = self.driver.page_source
            for div in res_divs:
                res, name = '', ''
                target_id = div.get_attribute('id')
                time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                url, name = self.get_url(content, target_id)
                if not url:
                    name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/span[@class="nor-src-wrap"]/text()'.format(target_id)))
                    if not name:
                        name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//a[@class="c-showurl "]/span/text()'.format(target_id)))
                        if not name:
                            name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//span[@class="nor-src-wrap"]/text()'.format(target_id)))
                else:
                    # name = ''
                    res = re.findall(r'{}'.format(host_url), url)
                    if not res:
                        res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)
                # print(res)
                if res or (name == name_url and name):  # 这段代码可以封装
                    if self.biao == 1:
                        continue
                    time.sleep(random.randint(3, 5))
                    element = WebDriverWait(self.driver, 30).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1,3))
                    element.click()
                    time.sleep(random.randint(20, self.timeout + 50))
                    handles = self.driver.window_handles  # 窗口句柄
                    self.driver.switch_to.window(handles[-1])
                    time.sleep(2)
                    self.driver.close() #关闭当前窗口
                    time.sleep(random.randint(1, 3))
                    handles = self.driver.window_handles  # 窗口句柄
                    self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                    time.sleep(random.randint(1, 5))
                    target_url_host.append(target_id)
                    logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                        res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + target_id + ',' + '点击状态:' + '经过模式目标网址点击完成,' + 'time:' + time_now + '}')
                    self.biao = 1
                else:
                    if random.randint(1, 101)% 12 == 0:
                        time.sleep(random.randint(1, 3))
                        element = WebDriverWait(self.driver, 30).until(
                            EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id))))
                        position = element.location_once_scrolled_into_view
                        time.sleep(0.5)
                        self.driver.execute_script(
                            "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                        time.sleep(1)
                        element.click()
                        handles = self.driver.window_handles  # 窗口句柄
                        time.sleep(random.randint(2, 6))
                        self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                        time.sleep(random.randint(1, 6))
                        logger.info(
                            '{ip:' + self.ip_port + ',' + '百度搜索关键词:' + self.keyword + ',' + '点击状态:' + '经过模式非目标网址点击完成,' + 'time:' + time_now + '}')
        else:
            logger.info('经过模式 ： 未找到指定域名！')
        return target_url_host

    def request_zhijie(self, host_url, name_url):  # 直接模式
        target_url_host = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        if not res_divs:
            res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container new-pmd"]')
        if res_divs:
            content = self.driver.page_source
            for div in res_divs:
                res, name = '', ''
                target_id = div.get_attribute('id')
                time_now = time.strftime("%H:%M:%S", time.localtime())
                url,name = self.get_url(content,target_id)
                if not url:
                    name = self.is_empty(
                        self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/span[@class="nor-src-wrap"]/text()'.format(target_id)))
                    if not name:
                        name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//a[@class="c-showurl "]/span/text()'.format(target_id)))
                        if not name:
                            name = self.is_empty(self.my_xpath(content,'//div[@id="{}"]//span[@class="nor-src-wrap"]/text()'.format(target_id)))
                    # print(name)
                else:
                    res = re.findall(r'{}'.format(host_url), url)
                    if not res:
                        res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)
                # print(target_id,url, name)
                if res or (name == name_url and name):  # 这段代码可以封装
                    time.sleep(random.randint(2, 4))
                    element = WebDriverWait(self.driver, 30).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1, 3))
                    element.click()
                    time.sleep(random.randint(20, self.timeout + 50))
                    target_url_host.append(target_id)
                    handles = self.driver.window_handles  # 窗口句柄
                    self.driver.switch_to.window(handles[-1])  # 切换到最初打开的窗口
                    # time.sleep(random.randint(self.timeout,self.timeout+30))
                    if random.randint(1, 101) <= 50:
                        self.driver.refresh()
                        time.sleep(random.randint(1, self.timeout))
                    logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                        res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + target_id + ',' + '点击状态:' + '直接模式目标网址点击完成,' + 'time:' + time_now + '}')
                    self.driver.close()
                    return target_url_host
        else:
            self.take_slide()
            logger.info('未找到指定域名！')
            return []

    def get_url(self,content,target_id):
        content_url = self.my_xpath(content, '//div[@id="{}"]//text()'.format(target_id))
        url = ''.join(content_url)
        name = ''
        if not url:
            url = self.is_empty(
                self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/text()'.format(target_id)))
            if not url:
                url = self.is_empty(self.my_xpath(content,
                                                  '//div[@id="{}"]//*[@class="c-showurl c-color-gray"]/text()'.format(
                                                      target_id)))
                if not url:
                    url = self.is_empty(
                        self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id)))
                    if not url:
                        url = self.is_empty(
                            self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
                        if not url:
                            url = self.is_empty(self.my_xpath(content,
                                                              '//div[@id="{}"]//div[@class="f13  se_st_footer"]/a[1]/text()'.format(
                                                                  target_id)))
                            if not url:
                                url = self.is_empty(self.my_xpath(content,
                                                                  '//div[@id="{}"]//a[@class="c-showurl"]/text()'.format(
                                                                      target_id)))
        if '.' not in url:
            name = url
            url = ''
        return url,name

    def chuli_gengduo(self,content):
        unfold = self.my_xpath(content, '//div[@class="unfold"]')
        if unfold:
            unfold = self.driver.find_element_by_xpath('//div[@class="unfold"]')
            time.sleep(0.5)
            unfold.click()
            time.sleep(0.5)
        unfold_2 = self.my_xpath(content, '//*[@id="results"]/section//span[@class="button-text"]')
        if unfold_2:
            print('点击获取更多页面！')
            unfold_2 = self.driver.find_element_by_xpath('//*[@id="results"]/section//span[@class="button-text"]')
            time.sleep(0.5)
            unfold_2.click()
            time.sleep(0.5)
            queren = self.driver.find_element_by_xpath('//*[@id="popupLead"]//div[@class="popup-lead-cancel"]')
            time.sleep(0.5)
            queren.click()
            time.sleep(0.5)

    def mobile_request_jiezi(self, host_url, name_url):  # 截止模式
        target_url_host = []
        target_id_li = []
        time.sleep(1)
        content = self.driver.page_source
        self.chuli_gengduo(content)
        res_divs = self.driver.find_elements_by_xpath('//div[contains(@class,"c-result result")]')
        if not res_divs:
            res_divs = self.driver.find_elements_by_xpath(
                '//div[@order]')
        if res_divs:
            for div in res_divs:
                target_id = div.get_attribute('order')
                target_id_li.append(target_id)
            for target_id in target_id_li:
                content = self.driver.page_source
                res, name = '', ''
                time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                url = self.my_xpath(content,
                                    '//div[@order="{}"]/div[@class="c-result-content"]//div[@class="c-flexbox"]//span/text()'.format(
                                        target_id))
                url = ''.join(url).strip()
                if '.' not in url:
                    name = url
                else:
                    res = re.findall(r'{}'.format(host_url), url)
                    if not res:
                        res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)
                if res or (name == name_url and name):  # 这段代码可以封装
                    time.sleep(3)
                    element = WebDriverWait(self.driver, 15).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@order="{}"]/div[@class="c-result-content"]/article'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1, 3))
                    element.click()
                    time.sleep(2)
                    self.run_js_canshu()
                    content = self.driver.page_source
                    detail_upload = self.my_xpath(content, '//div[@class="detail-unfold"]/div')
                    if detail_upload:
                        detail_upload = self.driver.find_element_by_xpath('//div[@class="detail-unfold"]/div/span[1]/i')
                        position = detail_upload.location_once_scrolled_into_view
                        time.sleep(1)
                        detail_upload.click()
                    time.sleep(random.randint(20, self.timeout+20))
                    if random.randint(1, 101) <= 60:
                        self.driver.refresh()
                        time.sleep(1)
                        self.run_js_canshu()
                        time.sleep(random.randint(1, 6))
                    if random.randint(1, 101)>=30:
                        self.driver.refresh()
                        time.sleep(1)
                        self.run_js_canshu()
                        time.sleep(random.randint(1, self.timeout))
                    current_rank = self.rank + self.get_interger(target_id)
                    target_url_host.append(current_rank)
                    self.rank = 0
                    logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                        res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + str(current_rank) + ',' + '点击状态:' + '截止模式目标网址点击完成,' + 'time:' + time_now + '，模式:手机模式 }')
                    self.driver.close()
                    return target_url_host
                else:
                    if random.randint(1, 100) % 6 == 0:
                        time.sleep(2)
                        element = WebDriverWait(self.driver, 15).until(
                            EC.presence_of_element_located((By.XPATH, '//div[@order="{}"]/div[@class="c-result-content"]/article'.format(target_id))))
                        position = element.location_once_scrolled_into_view
                        time.sleep(1)
                        self.driver.execute_script(
                            "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                        time.sleep(random.randint(1, 3))
                        element.click()
                        time.sleep(random.randint(1, 5))
                        self.run_js_canshu()
                        time.sleep(1)
                        self.driver.back()
                        time.sleep(1)
                        self.run_js_canshu()
                        content = self.driver.page_source
                        self.chuli_gengduo(content)
                        logger.info('{ip:' + self.ip_port + ',' + '百度搜索关键词:' + self.keyword + ',' + '点击状态:' + '截止模式非目标网址点击完成,' + 'time:' + time_now + '，模式:手机模式 }')
            self.rank = self.rank + len(res_divs)
        else:
            self.take_slide()
            logger.info('截止模式 ： 未找到指定域名！')
            return []

    def mobile_request_jingguo(self, host_url, name_url):  # 经过模式
        target_url_host = []
        target_id_li = []
        time.sleep(1)
        content = self.driver.page_source
        self.chuli_gengduo(content)
        res_divs = self.driver.find_elements_by_xpath('//div[contains(@class,"c-result result")]')
        if not res_divs:
            res_divs = self.driver.find_elements_by_xpath(
                '//div[@order]')
        if res_divs:
            for div in res_divs:
                target_id = div.get_attribute('order')
                target_id_li.append(target_id)
            for target_id in target_id_li:
                content = self.driver.page_source
                res, name = '', ''
                time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                url = self.my_xpath(content,
                                    '//div[@order="{}"]/div[@class="c-result-content"]//div[@class="c-flexbox"]//span/text()'.format(
                                        target_id))
                url = ''.join(url).strip()
                if '.' not in url:
                    name = url
                else:
                    res = re.findall(r'{}'.format(host_url), url)
                    if not res:
                        res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)

                if res or (name == name_url and name):  # 这段代码可以封装
                    if self.biao == 1:
                        continue
                    time.sleep(random.randint(3, 5))
                    element = WebDriverWait(self.driver, 15).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@order="{}"]/div[@class="c-result-content"]/article'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1,3))
                    element.click()
                    time.sleep(random.randint(1, 3))
                    self.run_js_canshu()
                    content = self.driver.page_source
                    detail_upload = self.my_xpath(content, '//div[@class="detail-unfold"]/div')
                    if detail_upload:
                        detail_upload = self.driver.find_element_by_xpath('//div[@class="detail-unfold"]/div/span[1]/i')
                        position = detail_upload.location_once_scrolled_into_view
                        time.sleep(1)
                        detail_upload.click()
                    time.sleep(random.randint(20, self.timeout + 50))
                    self.driver.back()
                    time.sleep(random.randint(1, 5))
                    self.run_js_canshu()
                    time.sleep(1)
                    content = self.driver.page_source
                    self.chuli_gengduo(content)
                    current_rank = self.rank + self.get_interger(target_id)
                    target_url_host.append(current_rank)
                    logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                        res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + str(current_rank) + ',' + '点击状态:' + '经过模式目标网址点击完成,' + 'time:' + time_now + '，模式:手机模式 }')
                    self.biao = 1
                else:
                    if random.randint(1, 101)% 12 == 0:
                        time.sleep(random.randint(1, 3))
                        element = WebDriverWait(self.driver, 15).until(
                            EC.presence_of_element_located((By.XPATH, '//div[@order="{}"]/div[@class="c-result-content"]/article'.format(target_id))))
                        position = element.location_once_scrolled_into_view
                        time.sleep(0.5)
                        self.driver.execute_script(
                            "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                        time.sleep(1)
                        element.click()
                        time.sleep(random.randint(1, 6))
                        self.run_js_canshu()
                        time.sleep(1)
                        self.driver.back()
                        time.sleep(random.randint(1, 4))
                        self.run_js_canshu()
                        content = self.driver.page_source
                        self.chuli_gengduo(content)
                        logger.info(
                            '{ip:' + self.ip_port + ',' + '百度搜索关键词:' + self.keyword + ',' + '点击状态:' + '经过模式非目标网址点击完成,' + 'time:' + time_now + '，模式:手机模式 }')
            self.rank = self.rank + len(res_divs)
        else:
            self.take_slide()
            logger.info('经过模式 ： 未找到指定域名！')
        return target_url_host

    def mobile_request_zhijie(self, host_url, name_url):  # 直接模式
        target_url_host = []
        time.sleep(1)
        content = self.driver.page_source
        self.chuli_gengduo(content)
        res_divs = self.driver.find_elements_by_xpath('//div[contains(@class,"c-result result")]')
        if not res_divs:
            res_divs = self.driver.find_elements_by_xpath(
                '//div[@order]')
        if res_divs:
            content = self.driver.page_source
            for div in res_divs:
                res, name = '', ''
                target_id = div.get_attribute('order')
                time_now = time.strftime("%H:%M:%S", time.localtime())
                url = self.my_xpath(content,'//div[@order="{}"]/div[@class="c-result-content"]//div[@class="c-flexbox"]//span/text()'.format(target_id))
                url = ''.join(url).strip()
                if '.' not in url:
                    name = url
                else:
                    res = re.findall(r'{}'.format(host_url), url)
                    if not res:
                        res = re.findall(r'{}'.format(host_url.replace('www.', '')), url)
                if res or (name == name_url and name):  # 这段代码可以封装
                    time.sleep(random.randint(2, 4))
                    element = WebDriverWait(self.driver, 15).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@order="{}"]/div[@class="c-result-content"]/article'.format(target_id))))
                    position = element.location_once_scrolled_into_view
                    time.sleep(1)
                    self.driver.execute_script(
                        "window.scrollTo({},{})".format(self.get_interger(position.get('y')),self.get_interger(position.get('y')) + 50))
                    time.sleep(random.randint(1, 3))
                    element.click()
                    time.sleep(random.randint(2, 5))
                    self.run_js_canshu()
                    content = self.driver.page_source
                    detail_upload = self.my_xpath(content,'//div[@class="detail-unfold"]/div')
                    if detail_upload:
                        detail_upload = self.driver.find_element_by_xpath('//div[@class="detail-unfold"]/div/span[1]/i')
                        position = detail_upload.location_once_scrolled_into_view
                        time.sleep(1)
                        detail_upload.click()
                    time.sleep(random.randint(20, self.timeout + 50))
                    current_rank = self.rank + self.get_interger(target_id)
                    target_url_host.append(current_rank)
                    self.rank = 0
                    handles = self.driver.window_handles  # 窗口句柄
                    self.driver.switch_to.window(handles[-1])  # 切换到最初打开的窗口
                    time.sleep(random.randint(self.timeout,self.timeout+30))
                    if random.randint(1, 101) <= 70:
                        self.driver.refresh()
                        time.sleep(2)
                        self.run_js_canshu()
                        time.sleep(random.randint(1, 6))
                    if random.randint(1, 101) <= 50:
                        self.driver.refresh()
                        time.sleep(2)
                        self.run_js_canshu()
                        time.sleep(random.randint(1, self.timeout))
                    logger.info('{ip:' + self.ip_port + ',目标域名:' + self.is_empty(
                        res) + ',' + '熊掌号name:' + name + ',' + '百度关键词排名:' + str(current_rank) + ',' + '点击状态:' + '直接模式目标网址点击完成,' + 'time:' + time_now + '，模式:手机模式 }')
                    self.driver.close()
                    return target_url_host
            self.rank = self.rank + len(res_divs)
        else:
            self.take_slide()
            logger.info('直接模式 ： 未找到指定域名！')
            return []

    def get_track(self,x):
        '''
        滑块移动轨迹
        初速度 v =0
        单位时间 t = 0.2
        位移轨迹 tracks = []
        当前位移 ccurrent = 0
        :param x:
        :return:
        '''
        v = 0
        t = random.randint(4,10)/10
        tracks = []
        current = 0
        # mid = x*5/8#到达mid值开始减速
        # x = x+10
        while current < x:
            # if current < mid:
            #     a = random.randint(1,3)
            # else:
            #     a = -random.randint(2,4)
            a1 = random.randint(8,16)
            a2 = random.randint(15,25)
            v0 = v
            # 单位时间内位移公式
            num = random.choice([0.5,0.6,0.7,0.8])
            if current <= num*x:
                s = v0 * t + 0.5 * a1 * (t ** 2)
                v = v0 + a1 * t
            else:
                s = v0 * t - 0.5 * a2 * (t ** 2)
                v = v0 - a2 * t
            if s < 0:
                return tracks
            # 当前位移
            current = current + s
            tracks.append(round(s))

        return tracks

    def take_slide(self,shu=0): #处理滑动验证
        num_shu = shu
        content = self.driver.page_source
        button = self.my_xpath(content, '//*[@class="vcode-slide-button"]')
        if button:
            print('需要验证！')
            try:
                self.driver.maximize_window()
                button_kuai = self.driver.find_element_by_class_name("vcode-slide-button")
                time.sleep(1)
                actions = ActionChains(self.driver)
                actions.click_and_hold(on_element=button_kuai).perform()
                time.sleep(random.randint(30,100)/100)
                for i in self.get_track(262):
                    actions.move_by_offset(xoffset=i,yoffset=0)
                time.sleep(1)
                actions.release(on_element=button_kuai).perform()
                time.sleep(3)
                actions.reset_actions()
                self.run_js_canshu()
                slibe_content = self.driver.page_source
                result = self.my_xpath(slibe_content,'//div[@class="vcode-slide-cover"]/p/text()')
                if result and result[0] == '验证失败':
                    num_shu += 1
                    if num_shu <= 5:
                        self.driver.refresh()
                        time.sleep(1)
                        self.run_js_canshu()
                        return self.take_slide(num_shu)
                    else:
                        raise Exception('百度验证没有通过！')
                else:
                    return '验证成功，将跳转！'
            except Exception as e:
                pass

    def click_next_page(self,moshi, func, host_url, name_url, page=0, number=0):
        # number 限制回调次数
        self.biao = 0
        shu = number
        shu += 1
        host_url_s = host_url
        name_url_s = name_url
        jingguo_rank_list = []
        for i in range(page, 10):
            url = self.driver.current_url
            try:
                res = func(host_url_s, name_url_s)
                if res and (func != self.request_jingguo and func != self.mobile_request_jingguo):
                    return res[0]
                else:
                    if res:
                        jingguo_rank_list.extend(res)
                time.sleep(2)
                if moshi == 1:
                    next_page = self.driver.find_element_by_xpath('//*[@id="page"]/div/a[last()]')
                    if func == self.request_zhijie:
                        time.sleep(random.randint(1, 5))
                else:
                    if i == 0:
                        try:
                            content = self.driver.page_source
                            self.chuli_gengduo(content)
                            next_page = WebDriverWait(self.driver, 10).until(
                                EC.presence_of_element_located((By.XPATH, '//*[@id="page-controller"]/div/a')))
                        except Exception as e:
                            self.take_slide()
                            time.sleep(2)
                            next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]/div/a')
                            if not next_page:
                                content = self.driver.page_source
                                self.chuli_gengduo(content)
                                time.sleep(2)
                                next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]/div/a')
                                if not next_page:
                                    self.driver.refresh()
                                    time.sleep(3)
                                    self.run_js_canshu()
                                    next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]/div/a')
                    else:
                        try:
                            content = self.driver.page_source
                            self.chuli_gengduo(content)
                            next_page = WebDriverWait(self.driver, 10).until(
                                EC.presence_of_element_located((By.XPATH, '//*[@id="page-controller"]//div[@class="new-pagenav-right"]/a')))
                        except Exception as e:
                            self.take_slide()
                            time.sleep(2)
                            next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]//div[@class="new-pagenav-right"]/a')
                            if not next_page:
                                content = self.driver.page_source
                                self.chuli_gengduo(content)
                                time.sleep(2)
                                next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]//div[@class="new-pagenav-right"]/a')
                                if not next_page:
                                    self.driver.refresh()
                                    time.sleep(3)
                                    self.run_js_canshu()
                                    next_page = self.driver.find_element_by_xpath('//*[@id="page-controller"]//div[@class="new-pagenav-right"]/a')
                if func == self.request_zhijie:
                    time.sleep(random.randint(1,3))
                position = next_page.location_once_scrolled_into_view
                time.sleep(2)
                next_page.click()
                time.sleep(2)
                self.run_js_canshu()
                if moshi == 1:
                    self.driver.set_page_load_timeout(15)
                else:
                    self.driver.set_page_load_timeout(30)
                time.sleep(5)
            except Exception as e:
                if shu <= 10:
                    logger.info('点击访问失败，将重新访问！{}'.format(e))
                    self.driver.get(url)
                    time.sleep(2)
                    self.run_js_canshu()
                    result = self.click_next_page(moshi,func, host_url_s, name_url_s, i, shu)
                    return result
                else:
                    self.driver.quit()
                    raise Exception('运行出错！{}'.format(e))
        if jingguo_rank_list:
            return jingguo_rank_list[0]

    def get_url_2(self,content,target_id):
        content_url = self.my_xpath(content, '//div[@id="{}"]//text()'.format(target_id))
        url = ''.join(content_url)
        name = ''
        if not url:
            url = self.is_empty(
                self.my_xpath(content, '//div[@id="{}"]//a[@class="c-showurl "]/text()'.format(target_id)))
            if not url:
                url = self.is_empty(self.my_xpath(content,
                                                  '//div[@id="{}"]//*[@class="c-showurl c-color-gray"]/text()'.format(
                                                      target_id)))
                if not url:
                    url = self.is_empty(
                        self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id)))
                    if not url:
                        url = self.is_empty(
                            self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
                        if not url:
                            url = self.is_empty(self.my_xpath(content,
                                                              '//div[@id="{}"]//div[@class="f13  se_st_footer"]/a[1]/text()'.format(
                                                                  target_id)))
                            if not url:
                                url = self.is_empty(self.my_xpath(content,
                                                                  '//div[@id="{}"]//a[@class="c-showurl"]/text()'.format(
                                                                      target_id)))
        if '.' not in url:
            name = url
            url = ''
        return url,name


    def exit_chrome(self):
        try:
            time.sleep(3)
            js = 'window.localStorage.clear();'
            self.driver.execute_script(js)
            time.sleep(2)
            self.clear_cookie()
        except Exception as e:
            pass
        time.sleep(5)
        self.driver.quit()

    def my_xpath(self, data, ruler):  # xpath打包方法
        return etree.HTML(data).xpath(ruler)

    def run(self, host_url, hostName, timeout, value,tup):
        self.timeout = self.get_interger(timeout)
        moshi = self.moshi
        # self.take_baidu_login(USER, PASSWORD)
        # self.driver.get('http://httpbin.org/get') #测试ip
        # time.sleep(5)
        rank = ''
        if random.randint(1, 101) <= 80:
            self.take_cookie(moshi,tup)
            # self.china_school_guanzhu()
        self.open_baidu(moshi,self.keyword)
        # self.make_baidu_page()
        time.sleep(5)
        print(value)
        if moshi==1:
            if value == 'zhijie':
                rank = self.click_next_page(moshi,self.request_zhijie, host_url, hostName)
            elif value == 'jingguo':
                self.click_next_page(moshi,self.request_jingguo, host_url, hostName)
            else:
                rank = self.click_next_page(moshi,self.request_jiezi, host_url, hostName)
        else:
            if value == 'zhijie':
                rank = self.click_next_page(moshi,self.mobile_request_zhijie, host_url, hostName)
            elif value == 'jingguo':
                self.click_next_page(moshi,self.mobile_request_jingguo, host_url, hostName)
            else:
                rank = self.click_next_page(moshi,self.mobile_request_jiezi, host_url, hostName)
        self.exit_chrome()
        return rank


if __name__ == '__main__':
    # run_kuaipai('人参生长周期','dongbeiw.com','','15','5','1','3','1')
    # run_kuaipai('迷你世界安卓','hcedu100.com','','15','5','3','1','1')

    cookies_list_1 = open_cookie_file('all2_cookies.json', 'a')
    cookies_list_2 = open_cookie_file('all_cookies.json', 'b')
    cookies_list_3 = open_cookie_file('all3_cookies.json', 'b')
    cookies_list_4 = open_cookie_file('all4_cookies.json', 'b')
    cookies_list_5 = open_cookie_file('all5_cookies.json', 'c')
    tup = (cookies_list_1, cookies_list_2, cookies_list_3, cookies_list_4, cookies_list_5)
    # a = KuaiPai('','','')
    # a.take_cookie_2(1, tup)
    run_kuaipai('女性吃人参的方法','dongbeiw.com','','15','12','5','1','4',tup)


